//
//  MyGradesCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/28.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MyGradesCell.h"
#import "LotteryNumCell.h"
@interface MyGradesCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *numCollectionView;
@property (nonatomic ,strong) UICollectionView *aliasCollectionView;
@property (nonatomic ,strong) QXLable          *labTime;
@end

@implementation MyGradesCell{
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle=UITableViewCellSeparatorStyleNone;
        self.labTime =[[QXLable alloc]init];
        self.labTime.textAlignment=NSTextAlignmentCenter;
        self.labTime.font=[UIFont systemFontOfSize:10];
        [self.contentView addSubview:self.labTime];
        [self.labTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.width.mas_equalTo(@100);
            make.centerY.equalTo(self.contentView);
        }];
        
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTLINECOLOR;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.left.equalTo(self.contentView).with.offset(100);
            make.width.mas_equalTo(@.7);
            make.bottom.equalTo(self.contentView);
        }];
        
        [self.contentView addSubview:self.numCollectionView];
        [self.numCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(line).with.offset(15);
            make.top.equalTo(self.contentView).with.offset(5);
            make.right.equalTo(self.contentView);
            make.height.mas_equalTo(@27);
        }];
        
        [self.contentView addSubview:self.aliasCollectionView];
        [self.aliasCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.numCollectionView);
            make.top.equalTo(self.numCollectionView.mas_bottom);
            make.right.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView).with.offset(-5);
            make.height.mas_equalTo(@25);
        }];
        
        UIView *line1=[[UIView alloc]init];
        line1.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.contentView);
            make.height.mas_equalTo(@.7);
        }];
        
        UIView *line2=[[UIView alloc]init];
        line2.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(@.7);
        }];
    }
    return self;
}

- (void)setModel:(GradesModel *)model{
    _model=model;
    self.labTime.text=[NSString stringWithFormat:@"%@期\n%@",model.LotteryNum.Issue,model.LotteryNum.SubTime];
    
    if (self.model.LotteryNum.NumAliasArr.count>1) {
        [self.aliasCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@20);
        }];
    }else{
        [self.aliasCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0);
        }];
    }
    
    [self.numCollectionView reloadData];
    [self.aliasCollectionView reloadData];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView==self.aliasCollectionView) {
        return self.model.LotteryNum.NumAliasArr.count;
    }
    if (self.model.LotteryNum.lType==65) {
        return self.model.LotteryNum.PCArr.count;
    }else if ([[BaseVM manage]getType:self.model.LotteryNum.lType]==LotteryNumTypeLottery){
        return self.model.LotteryNum.LotteryArr.count;
    }
    return self.model.LotteryNum.NumArr.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LotteryNumCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LotteryNumCell" forIndexPath:indexPath];
    if (collectionView==self.aliasCollectionView) {
        [cell setDownTitle:self.model.LotteryNum.NumAliasArr[indexPath.row] andType:[[BaseVM manage] getType:self.type]];
    }else{
        LotteryNumType type=[[BaseVM manage]getType:self.model.LotteryNum.lType];
        if (self.model.LotteryNum.lType==65) {
            [cell setTitle:self.model.LotteryNum.PCArr[indexPath.row] andType:type];
        }else if (type==LotteryNumTypeLottery){
            [cell setTitle:self.model.LotteryNum.LotteryArr[indexPath.row] andType:type];
        }else{
            [cell setTitle:self.model.LotteryNum.NumArr[indexPath.row] andType:type];
        }
        if ((type==LotteryNumTypeChromosphere||type==LotteryNumTypeBrings||type==LotteryNumTypeSevenLecai)&&(indexPath.row==self.model.LotteryNum.NumArr.count-1)) {
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }else if (type==LotteryNumTypeBrings&&(indexPath.row==self.model.LotteryNum.NumArr.count-2)){
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }
        
//
//        [cell setTitle:self.model.LotteryNum.NumArr[indexPath.row] andType:[[BaseVM manage] getType:self.type]];
    }
    [cell setFount:15];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 3, 0, 3);
}

#pragma mark -- 懒加载
- (UICollectionView *)numCollectionView{
    if (_numCollectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(27, 27)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _numCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _numCollectionView.delegate = self;
        _numCollectionView.dataSource = self;
        _numCollectionView.backgroundColor=[UIColor whiteColor];
        _numCollectionView.showsVerticalScrollIndicator=NO;
        [_numCollectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
    }
    return _numCollectionView;
}

- (UICollectionView *)aliasCollectionView{
    if (_aliasCollectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(35, 20)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _aliasCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _aliasCollectionView.delegate = self;
        _aliasCollectionView.dataSource = self;
        _aliasCollectionView.backgroundColor=[UIColor whiteColor];
        _aliasCollectionView.showsVerticalScrollIndicator=NO;
        [_aliasCollectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
    }
    return _aliasCollectionView;
}

@end
